import 'package:flutter/material.dart';

class GradientButton extends StatelessWidget {
  // 渐变色数组
  final List<Color>? colors;

  final double? width;
  final double? height;

  final Widget child;
  final BorderRadius? borderRadius;

  final int quarterTurns;

  //点击回调
  final GestureTapCallback? onPressed;

  const GradientButton(
      {Key? key,
        this.colors,
        this.width,
        this.height,
        required this.child,
        this.quarterTurns = 0,
        this.borderRadius,
        this.onPressed})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    ThemeData theme = Theme.of(context);

    List<Color> _colors =
        colors ?? [theme.primaryColor, theme.primaryColorDark];

    return RotatedBox(
      quarterTurns: quarterTurns,
      child: DecoratedBox(
          decoration: BoxDecoration(
            gradient: LinearGradient(colors: _colors),
            borderRadius: borderRadius,
          ),
          child: Material(
            type: MaterialType.transparency,
            child: InkWell(
                borderRadius: borderRadius,
                splashColor: _colors.last,
                highlightColor: Colors.transparent,
                onTap: onPressed,
                child: ConstrainedBox(
                    constraints:
                    BoxConstraints.tightFor(width: width, height: height),
                    child: Center(
                        child: Padding(
                          padding: const EdgeInsets.all(8),
                          child: DefaultTextStyle(
                              style: TextStyle(fontWeight: FontWeight.bold),
                              child: child),
                        )))),
          )),
    );
  }
}
